package com.chahu.service.impl;

import com.chahu.entity.User;
import com.chahu.mapper.UserMapper;
import com.chahu.service.UserService;
import com.chahu.utils.MybatisUtil;
import org.apache.ibatis.exceptions.PersistenceException;
import org.apache.ibatis.session.SqlSession;

import javax.servlet.http.HttpSession;
import java.util.Collections;
import java.util.List;

public class UserServiceImpl implements UserService {

    @Override
    public boolean auth(String username, String password, HttpSession session) {
        try (SqlSession sqlSession = MybatisUtil.getFactory().openSession(true)) {
            UserMapper mapper = sqlSession.getMapper(UserMapper.class);
            User user = mapper.getUser(username, password);
            if(user == null) return false;
            session.setAttribute("user", user);
        }
        return true;
    }
    @Override
    public boolean register(User user, HttpSession session) {
        try (SqlSession sqlSession = MybatisUtil.getFactory().openSession(true)) {
            UserMapper mapper = sqlSession.getMapper(UserMapper.class);
            try {
                mapper.registerUser(user);
            } catch (PersistenceException e) {
                System.out.println("插入失败，异常信息：" + e.getMessage());
                System.out.println(user);
                return false;
            }
        }
        return true;
    }

    @Override
    public List<User> page(int page, int pageSize) {
        try (SqlSession sqlSession = MybatisUtil.getFactory().openSession(true)) {
            UserMapper mapper = sqlSession.getMapper(UserMapper.class);
            List<User> userList = mapper.getUserList(page, pageSize);
            if(userList == null){
                return Collections.emptyList();
            }
            return userList;
        }
    }
	@Override
	public boolean login(String name, String pwd) {
		try (SqlSession sqlSession = MybatisUtil.getFactory().openSession(true)) {
            UserMapper mapper = sqlSession.getMapper(UserMapper.class);
            int role = mapper.login(name, pwd);
            return role == 1;
        }
	}
	@Override
	public boolean updateById(User user) {
		try (SqlSession sqlSession = MybatisUtil.getFactory().openSession(true)) {
            UserMapper mapper = sqlSession.getMapper(UserMapper.class);
            try {
            	mapper.updateById(user);
            } catch (PersistenceException e) {
                System.out.println("更新失败，异常信息：" + e.getMessage());
                return false;
            }
        }
		return true;
	}
	@Override
	public boolean deleteById(int id) {
		try (SqlSession sqlSession = MybatisUtil.getFactory().openSession(true)) {
            UserMapper mapper = sqlSession.getMapper(UserMapper.class);
            try {
            	mapper.deleteById(id);
            } catch (PersistenceException e) {
                System.out.println("删除失败，异常信息：" + e.getMessage());
                return false;
            }
        }
		return true;
	}
}
